import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { Language } from "../../../../../extend/language/Language";
import { MoneyItem2 } from "../../common/item/MoneyItem2";
import { DataEvent } from "../../../events/DataEvent";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { EquipAdvanceConfig } from "../../../resource/EquipAdvanceConfig";
import { Cache } from "../../../cache/Cache";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { MsgManager } from "../../../manager/MsgManager";
import { EquipUpdateParams } from "../data/EquipUpdateParams";
import { EItemUpdateOper } from "../../../../../Message/Game/EItemUpdateOper";
import { EEquipRefreshOperType } from "../../../../../Message/Game/EEquipRefreshOperType";
import { EEquipRefreshNumType } from "../../../../../Message/Game/EEquipRefreshNumType";
import { BatchRefreshData } from "../data/BatchRefreshData";
import { ItemData } from "../../../resource/info/ItemData";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { ECategory } from "../../../../../Message/Public/ECategory";
import { EProp } from "../../../../../Message/Public/EProp";
import { EquipmentItemDataUtil } from "../../common/EquipmentItemDataUtil";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { ShopConfig } from "../../../resource/ShopConfig";
import { EPrictUnit } from "../../../../../Message/Public/EPrictUnit";
import { GameDefConfig } from "../../../resource/GameDefConfig";
import { ShopType } from "../../../resource/info/ShopType";
import { ItemConfig } from "../../../resource/ItemConfig";
import { EBind } from "../../../../../Message/Public/EBind";
import { ShopRule } from "../../../rules/ShopRule";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { ParamsConst } from "../../../../common/global/ParamsConst";
import { PulseSharedObject } from "../../player/PulseSharedObject";
import { TodayNoTipsConst } from "../../../../component/gconst/TodayNoTipsConst";
import { Global } from "../../../../../com/gengine/global/Global";
import { GuideTipsManager } from "../../../manager/GuideTipsManager";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { ExAttributePanel } from "../deploy/ExAttributePanel";
import { TargetRefreshSelectPanel } from "../deploy/TargetRefreshSelectPanel";
import { TilePanel } from "../TilePanel";
import { EquipBatchRefreshRender } from "./EquipBatchRefreshRender";
import { RefreshMaterialItem } from "./RefreshMaterialItem";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { ButtonType } from "../../../manager/ButtonType";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
import { GButton } from "../../../../../com/mui/controls/GButton";
type int = number;
//class BatchRecastWindow
    
    export  class BatchRecastWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this.setSize(795, 485);
            this.title = Language.getString(61075);
            return;
        }

        /* internal  */createMoneyItem(arg1: int, arg2: int, arg3: int): MoneyItem2
        {
            var loc1=null;
            (loc1 = new MoneyItem2()).unit = arg1;
            loc1.x = arg2;
            loc1.y = arg3;
            this.addChild(loc1);
            return loc1;
        }

        /* internal  */onUseLockChange(arg1: DataEvent): void
        {
            this._equipExPanel.sortLabels();
            return;
        }

        /* internal  */onOptButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=0;
            if (!this._equip) 
            {
                return;
            }
            if (this.checkHasExpectAttribute()) 
            {
                Alert.show(Language.getString(61082), Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onOptAlertCloseHandler);
                return;
            }
            var loc1=EquipAdvanceConfig.instance.getRefreshConfigInfo(this._equip.itemInfo.item.color);
            if (loc1) 
            {
                loc2 = Cache.instance.role.money.coinBind + Cache.instance.role.money.coin;
                if (loc1.cost * 8 > loc2) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, loc1.cost * 8 - loc2));
                    MsgManager.showRollTipsMsg(Language.getString(60121));
                    return;
                }
            }
            this.doBatchRefresh();
            return;
        }

        /* internal  */onOptAlertCloseHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.doBatchRefresh();
            }
            return;
        }

        /* internal  */doBatchRefresh(): void
        {
            var loc1=new EquipUpdateParams();
            loc1.operationId = EItemUpdateOper._EItemUpdateOperRecast;
            loc1.equipPosType = this._equip.posType;
            loc1.equipUid = this._equip.uid;
            loc1.operType = EEquipRefreshOperType._EEquipRefreshNormal;
            loc1.flag = this._equipExPanel.lockNum > 0 ? true : false;
            loc1.lockPos = this._equipExPanel.lockPosDic;
            loc1.materials = this.getMaterials();
            loc1.refreshNumType = EEquipRefreshNumType._EEquipRefreshNumBatch;
            loc1.expectAttr = this._targetSelectPanel.expectAttr;
            loc1.replaceIndex = 0;
            loc1.expectAttrAll = this._targetSelectPanel.expectAttrAll;
            loc1.autoBuyTone = this._autoBuyStoneCb.visible && this._autoBuyStoneCb.selected;
            loc1.autoBuyLock = this._autoBuyLockCb.visible && this._autoBuyLockCb.selected;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Equipment_DoBatchRefresh, loc1));
            return;
        }

        /* internal  */checkHasExpectAttribute(): Boolean
        {
            var loc2=null;
            var loc1=this._listPanel.dataProvider;
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                loc2 = loc1.getItemAt(loc3) as BatchRefreshData;
                if (loc2 && loc2.hasExpectAttr) 
                {
                    return true;
                }
                ++loc3;
            }
            return false;
        }

        /* internal  */getMaterials(): Array<any>
        {
            var loc4=null;
            var loc1=Cache.instance.pack.backPackCache.getPropItems();
            var loc2=this._isPetEquip ? Cache.instance.equipment.isUseBind_petEquip : Cache.instance.equipment.isUseBind_equip;
            if (loc2) 
            {
                loc1.sortOn("bind", Array.DESCENDING);
            }
            var loc3=[];
            var loc5=0;
            while (loc5 < loc1.length) 
            {
                loc4 = loc1[loc5] as ItemData;
                if (!(!loc2 && ItemsUtil.isBind(loc4))) 
                {
                    if (this._equip.category == ECategory._ECategoryEquip && loc4.category == ECategory._ECategoryProp && loc4.itemInfo.item.type == EProp._EPropRefresh && loc4.itemInfo.item.color == this._equip.itemInfo.item.color) 
                    {
                        loc3.push(loc4.uid);
                    }
                    else if (this._equip.category == ECategory._ECategoryPetEquip && loc4.category == ECategory._ECategoryProp && loc4.itemInfo.item.type == EProp._EPropPetEquipRefresh && loc4.itemInfo.item.color == this._equip.itemInfo.item.color) 
                    {
                        loc3.push(loc4.uid);
                    }
                    else if (loc4.category == ECategory._ECategoryProp && loc4.itemInfo.item.type == EProp._EPropRefreshLock) 
                    {
                        loc3.push(loc4.uid);
                    }
                }
                ++loc5;
            }
            return loc3;
        }

        public updateMoney(arg1=null): void
        {
            var loc1=Cache.instance.role.money;
            this._moneyGold.value = loc1.gold;
            this._moneyCoin.value = loc1.coin;
            this._moneyCoinBind.value = loc1.coinBind;
            return;
        }

        public updateMaterialAmount(arg1=null): void
        {
            var loc1=this._isPetEquip ? Cache.instance.equipment.isUseBind_petEquip : Cache.instance.equipment.isUseBind_equip;
            this._material1.updateAmount(loc1);
            this._material2.updateAmount(loc1);
            return;
        }

        public setEquip(arg1: ItemData): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc5=null;
            var loc6=null;
            var loc7=0;
            var loc8=null;
            var loc9=null;
            var loc1=false;
            if (this._equip && arg1 && this._equip.itemCode == arg1.itemCode) 
            {
                loc1 = true;
            }
            this._equip = arg1;
            if (arg1) 
            {
                if (this._equipUtil) 
                {
                    this._equipUtil.itemData = arg1;
                }
                else 
                {
                    this._equipUtil = new EquipmentItemDataUtil(arg1);
                }
                this._equipExPanel.setAttributes(this._equipUtil);
                if (this._dataprovider) 
                {
                    this._dataprovider.removeAll();
                }
                else 
                {
                    this._dataprovider = new DataProvider();
                }
                if (arg1.itemExInfo.refDict) 
                {
                    loc3 = this._targetSelectPanel.expectAttr;
                    loc4 = this._targetSelectPanel.expectAttrAll;
                    loc5 = arg1.itemExInfo.refDict;
                    loc7 = 0;
                    while (loc7 < loc5.length) 
                    {
                        if ((loc6 = loc5[loc7] as Array<any>) && loc6.length > 0) 
                        {
                            loc2 = new BatchRefreshData();
                            loc2.index = loc7;
                            loc2.equipData = arg1;
                            loc2.attributes = loc6;
                            loc2.expectAttr = loc3;
                            loc2.expectAttrAll = loc4;
                            this._dataprovider.addItem(loc2);
                        }
                        ++loc7;
                    }
                }
                this._listPanel.dataProvider = this._dataprovider;
                if (!loc1) 
                {
                    this._targetSelectPanel.filterAttributes(arg1);
                    if (arg1.category != ECategory._ECategoryEquip) 
                    {
                        if (arg1.category == ECategory._ECategoryPetEquip) 
                        {
                            this._isPetEquip = true;
                            if (loc8 = ShopConfig.instance.getGoldItemByType(ECategory._ECategoryProp, EProp._EPropPetEquipRefresh, -1, EPrictUnit._EPriceUnitGold, GameDefConfig.instance.getEShopCode(ShopType.PetForge), arg1.color)) 
                            {
                                this._material1.updateData(loc8);
                                this._autoBuyStoneCb.visible = true;
                            }
                            else 
                            {
                                loc9 = ItemConfig.instance.getItemInfoByParams(ECategory._ECategoryProp, EProp._EPropPetEquipRefresh, EBind._EBindNo, -1, arg1.color);
                                this._material1.updateData(new ItemData(loc9.item.code));
                                this._autoBuyStoneCb.visible = false;
                            }
                        }
                    }
                    else 
                    {
                        this._isPetEquip = false;
                        if (loc8 = ShopConfig.instance.getGoldItemByType(ECategory._ECategoryProp, EProp._EPropRefresh, -1, EPrictUnit._EPriceUnitGold, ShopRule.ShopMallMaterial, arg1.color)) 
                        {
                            this._material1.updateData(loc8);
                            this._autoBuyStoneCb.visible = true;
                        }
                        else 
                        {
                            loc9 = ItemConfig.instance.getItemInfoByParams(ECategory._ECategoryProp, EProp._EPropRefresh, EBind._EBindNo, -1, arg1.color);
                            this._material1.updateData(new ItemData(loc9.item.code));
                            this._autoBuyStoneCb.visible = false;
                        }
                    }
                }
                this.updateMaterialAmount();
            }
            return;
        }

        public setLockPos(arg1: Map<any, any> /* flash.utils.Dictionary */): void
        {
            this._equipExPanel.lockPosDic = arg1;
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updateMaterialAmount);
            if (ParamsConst.Version == "1.0.5.5" && !PulseSharedObject.getValue(TodayNoTipsConst.GuideEquipBatchRefresh)) 
            {
                this._guideRefresh = true;
                if (!Global.isDebugModle) 
                {
                    PulseSharedObject.save(TodayNoTipsConst.GuideEquipBatchRefresh, true);
                }
                this.showGuideBatchRefresh();
            }
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updateMaterialAmount);
            if (this._guideRefresh) 
            {
                this.hideGuideBatchRefresh();
            }
            return;
        }

        /* internal  */showGuideBatchRefresh(): void
        {
            var loc1=null;
            loc1 = GuideTipsManager.getGuideTips(GuideTipsManager.Dir_BR, Language.getString(61081));
            loc1.x = 35;
            loc1.y = 230;
            this.addChild(loc1);
            return;
        }

        /* internal  */hideGuideBatchRefresh(): void
        {
            GuideTipsManager.hideGuideTips();
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            ObjCreate.createBitmap(ImagesConst.Fire, 100, 30, this);
            ObjCreate.createLabel(Language.getString(61076), 120, 35, 630, 20, flash.text.TextFormatAlign.LEFT, this, true, GlobalStyle.color1Uint);
            ObjCreate.createLoadedBitmap("BatchRecastBigBg.png", 18, 60, this);
            ObjCreate.createBg(20, 62, 160, 25, this, ImagesConst.TileBg);
            ObjCreate.createBg(20, 213, 160, 25, this, ImagesConst.TileBg);
            ObjCreate.createLoadedBitmap("CurExAttribute.png", 46, 67, this);
            ObjCreate.createLoadedBitmap("SelectExAttribute.png", 46, 218, this);
            this._equipExPanel = new ExAttributePanel();
            this._equipExPanel.x = 30;
            this._equipExPanel.y = 52;
            this._equipExPanel.type = ExAttributePanel.CurrentAttri;
            this._equipExPanel.showBg = false;
            this._equipExPanel.addEventListener(ExAttributePanel.UseLockChange, this.onUseLockChange);
            this.addChild(this._equipExPanel);
            this._targetSelectPanel = new TargetRefreshSelectPanel();
            this._targetSelectPanel.x = 30;
            this._targetSelectPanel.y = 240;
            this.addChild(this._targetSelectPanel);
            var loc1=0;
            while (loc1 < 8) 
            {
                ObjCreate.createBg(194 + 146 * loc1 % 4, 60 + 185 * Math.floor(loc1 / 4), 145, 182, this);
                ++loc1;
            }
            this._listPanel = new TilePanel();
            this._listPanel.verticalGap = 3;
            this._listPanel.horizontalGap = 1;
            this._listPanel.move(194, 60);
            this._listPanel.setSize(590, 370);
            this._listPanel.columnWidth = 145;
            this._listPanel.rowHeight = 182;
            this._listPanel.setStyle("cellRenderer", EquipBatchRefreshRender);
            this.addChild(this._listPanel);
            ObjCreate.createLabel(Language.getString(61077), 40, 365, 200, 20, flash.text.TextFormatAlign.LEFT, this, true, GlobalStyle.color5Uint);
            this._moneyGold = this.createMoneyItem(EPrictUnit._EPriceUnitGold, 34, 395);
            this._moneyCoin = this.createMoneyItem(EPrictUnit._EPriceUnitCoin, 34, 420);
            this._moneyCoinBind = this.createMoneyItem(EPrictUnit._EPriceUnitCoinBind, 34, 445);
            ObjCreate.createLabel(Language.getString(60231) + "：", 193, 442, 150, 20, flash.text.TextFormatAlign.LEFT, this, true, 16776960);
            this._material1 = new RefreshMaterialItem();
            this._material1.x = 255;
            this._material1.y = 435;
            this.addChild(this._material1);
            this._material2 = new RefreshMaterialItem();
            this._material2.x = 400;
            this._material2.y = 435;
            this.addChild(this._material2);
            this._material2.updateData(ShopConfig.instance.getGoldItemByType(ECategory._ECategoryProp, EProp._EPropRefreshLock, -1, EPrictUnit._EPriceUnitGold, ShopRule.ShopMallMaterial));
            this._autoBuyStoneCb = ObjCreate.createCheckBox(Language.getString(61078), 544, 432, 150, 20, this);
            this._autoBuyStoneCb.textField.filters = [FilterConst.glowFilter];
            this._autoBuyLockCb = ObjCreate.createCheckBox(Language.getString(61079), 544, 452, 150, 20, this);
            this._autoBuyLockCb.textField.filters = [FilterConst.glowFilter];
            this._optBtn = ObjCreate.createGButton(Language.getString(61080), 690, 437, 80, 30, this, ButtonType.ButtonRed);
            this._optBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOptButtonClick);
            return;
        }

        /* internal */ /* var */_equip: ItemData;

        /* internal */ /* var */_equipUtil: EquipmentItemDataUtil;

        /* internal */ /* var */_equipExPanel: ExAttributePanel;

        /* internal */ /* var */_targetSelectPanel: TargetRefreshSelectPanel;

        /* internal */ /* var */_dataprovider: DataProvider;

        /* internal */ /* var */_listPanel: TilePanel;

        /* internal */ /* var */_moneyGold: MoneyItem2;

        /* internal */ /* var */_moneyCoin: MoneyItem2;

        /* internal */ /* var */_moneyCoinBind: MoneyItem2;

        /* internal */ /* var */_material1: RefreshMaterialItem;

        /* internal */ /* var */_material2: RefreshMaterialItem;

        /* internal */ /* var */_autoBuyStoneCb: GCheckBox;

        /* internal */ /* var */_autoBuyLockCb: GCheckBox;

        /* internal */ /* var */_optBtn: GButton;

        /* internal */ /* var */_isPetEquip: Boolean;

        /* internal */ /* var */_guideRefresh: Boolean=false;
    }
